#
# Plugin.py
# (c) 2008 Will Baker
#

import imp, os

class PluginError:
    """
    A Plugin error.
    """
    def __init__( self, message ):
        self.message = message
        
    def __repr__( self ):
        return self.message

class Plugin:
    """
    A Plugin for loading a Comic.
    """
    def __init__( self, path ):
        self.path = path
        self.extensions = []
        self.load_all_pages = None
        self.pool_size = 0
        
        (self.name, ext) = os.path.splitext( os.path.basename(path) )
        
        try:
            self.module = imp.load_source( self.name, path )
            register = getattr( self.module, 'opentome_register_plugin' )
            variables = register()
            
            self.extensions       = variables['extensions']
            self.load_all_pages   = variables['load_all_pages']
            self.load_single_page = variables['load_single_page']
            self.pool_size        = variables['pool']
            
        except AttributeError, e:
            raise PluginError( "Couldn't find 'opentome_register_plugin' in " + path )
        except KeyError, e:
            raise PluginError( "Error accessing registration variable: " + repr(e) )
        except Exception, e:
            raise PluginError( "Error loading plugin: " + repr(e) )